package com.zztl.infiniteBand.ui.view.dialog

import android.graphics.drawable.ColorDrawable
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.LinearLayout
import android.widget.PopupWindow
import android.widget.TextView
import butterknife.BindView
import butterknife.ButterKnife
import butterknife.OnClick
import com.chad.library.adapter.base.BaseQuickAdapter
import com.zztl.infiniteBand.R
import com.zztl.infiniteBand.adapter.StringAdapter
import com.zztl.infiniteBand.base.BaseActivity
import com.zztl.infiniteBand.util.UiUtils
import java.util.*


/**
 * Created by lsm on 2016/9/9.
 */

class PhotoPop(private val context: BaseActivity, private val itemClickListener: PopOnItemClickListener, private val type: Int  //1代表纯文本对话框  ，2代表设备列表对话框
               , private val isShowTitle: Boolean  //是否显示标题
) : PopupWindow(context) {

    @BindView(R.id.recycleView)
    lateinit var mRecyclerView: RecyclerView
    @BindView(R.id.tv_title)
    lateinit var tvTitle: TextView
    @BindView(R.id.line)
    lateinit var lineView: View
    private var mParams: WindowManager.LayoutParams? = null
    private val itemList = ArrayList<String>()

    private var mAdapter: StringAdapter? = null
    private val title: String? = null
    private var mDismissListener: DismissListener? = null


    init {
        val conentView = LayoutInflater.from(context).inflate(R.layout.pop_photo, null)
        this.contentView = conentView
        ButterKnife.bind(this, conentView)
        initPop()
    }


    private fun initPop() {


        this.width = LinearLayout.LayoutParams.MATCH_PARENT
        // 设置SelectPicPopupWindow弹出窗体的高
        this.height = LinearLayout.LayoutParams.WRAP_CONTENT
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.isFocusable = true
        this.isOutsideTouchable = true
        // 刷新状态
        this.update()
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
        this.setBackgroundDrawable(ColorDrawable(UiUtils.getColor(R.color.color_7F8C8D)))
        this.animationStyle = android.R.style.Animation_Dialog
        /*  this.setOnDismissListener(() -> {
            mParams.alpha = 1f;
            context.getWindow().setAttributes(mParams);
        });*/
        tvTitle.visibility = if (isShowTitle) View.VISIBLE else View.GONE
        lineView.visibility = if (isShowTitle) View.VISIBLE else View.GONE
        val manager = LinearLayoutManager(context)
        manager.orientation = LinearLayoutManager.VERTICAL
        mRecyclerView.layoutManager = manager
        if (type == 1) {
            mAdapter = StringAdapter(R.layout.item_pop, itemList)
            setItemClickListener(mAdapter!!)
        }

    }

    private fun setItemClickListener(adapter: BaseQuickAdapter<*, *>) {
        mRecyclerView.adapter = adapter
        adapter.onItemClickListener = BaseQuickAdapter.OnItemClickListener { _, _, position ->
            itemClickListener.onItemClick(position)
            //                dismiss();
        }
    }


    fun setWindowParams() {
        mParams = context.window.attributes
        mParams!!.alpha = 0.6f
        context.window.attributes = mParams
        //        return this;
    }

    fun setDiamissListener(dismissListener: DismissListener): PhotoPop {
        this.mDismissListener = dismissListener
        return this
    }


    fun setData(list: List<String>): PhotoPop {
        mAdapter!!.setNewData(list)
        return this
    }


    @OnClick(R.id.btn_cancle)
    fun onClick(view: View) {
        when (view.id) {
            R.id.btn_cancle -> dismiss()
            else -> {
            }
        }
    }

    /**
     * 显示popupWindow
     */
    fun showPopupWindow() {
        setWindowParams()
        if (!this.isShowing) {
            // 以下拉方式显示popupwindow
            this.showAtLocation(context.window.decorView, Gravity.BOTTOM, 0, 0)
        }
    }

    interface PopOnItemClickListener {
        fun onItemClick(position: Int)
    }

    override fun dismiss() {
        if (null != mDismissListener) {
            mDismissListener!!.dismiss()
        }
        mParams!!.alpha = 1f
        context.window.attributes = mParams
        super.dismiss()
    }

}